import time
import pymysql
from selenium import webdriver
from selenium.webdriver.common.by import By
from unittest import TestCase


class TestLoan(TestCase):

    def testLoan1(self):
        driver = webdriver.Chrome()

        driver.maximize_window()

        driver.get("http://localhost:90")

        time.sleep(2)

        ele1 = driver.find_element(By.XPATH, "//input[@id='username']")
        ele1.send_keys("lisi")

        ele2 = driver.find_element(By.XPATH, "//input[@id='password']")
        ele2.send_keys("123456")

        ele3 = driver.find_element(By.XPATH, "//button[@id='login_btn']")
        ele3.click()

        time.sleep(3)

        driver.find_element(By.XPATH, '/html/body/div/div/aside/div[2]/nav/ul/li[4]/a').click()
        time.sleep(2)

        driver.find_element(By.XPATH, '/html/body/div/div/aside/div[2]/nav/ul/li[4]/ul/li[2]/a').click()
        time.sleep(2)

        text = driver.find_element(By.XPATH,
                                   '/html/body/div/div/main/div/div/div/div/div[2]/div[2]/div[2]/div/button[6]/span').text
        x1 = int(text.strip("条"))

        ele4 = driver.find_element(By.XPATH, '/html/body/div/div/aside/div[2]/nav/ul/li[3]/a')
        ele4.click()
        time.sleep(2)
        ele5 = driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/ul/li[2]/a')
        ele5.click()
        time.sleep(2)

        ele6 = driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[1]/input')
        ele6.send_keys("10000")
        time.sleep(2)

        ele7 = driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[3]/input')
        ele7.send_keys("10")
        time.sleep(2)

        ele8 = driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[4]/button')
        ele8.click()
        time.sleep(2)

        ele9 = driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]')
        ele9.click()
        time.sleep(2)

        driver.find_element(By.XPATH, '/html/body/div/div/aside/div[2]/nav/ul/li[4]/a').click()
        time.sleep(2)

        driver.find_element(By.XPATH, '/html/body/div/div/aside/div[2]/nav/ul/li[4]/ul/li[2]/a').click()
        time.sleep(2)
        #
        # WebDriverWait(driver,30,0.2).until(expected_conditions.alert_is_present())
        # x=driver.switch_to.alert.text
        # print(x)
        # result = driver.find_element(By.XPATH, "//div[@id='logo']/a").text

        # if result != "个人理财系统":
        #     driver.save_screenshot("lisi-123456.jpg")
        #
        # self.assertEqual(result, "个人理财系统")

        text = driver.find_element(By.XPATH,
                                   '/html/body/div/div/main/div/div/div/div/div[2]/div[2]/div[2]/div/button[6]/span').text
        x2 = int(text.strip("条"))

        if x2 != x1 + 1:
            driver.save_screenshot("贷款1.jpg")

        self.assertEqual(x1 + 1, x2)

        driver.quit()

    def testLoan2(self):
        connect = pymysql.connect(host='localhost', user='root', password='123456', database='finance')
        cursor = connect.cursor()
        sql = ' select id from user where username="lisi"'
        cursor.execute(sql)
        x = cursor.fetchall()
        id = x[0][0]
        sql = f'select balance from bankcard where defaultl=1 and userId={id}'
        cursor.execute(sql)
        y = cursor.fetchall()
        money1 = y[0][0]
        driver = webdriver.Chrome()

        driver.maximize_window()

        driver.get("http://localhost:90")

        time.sleep(2)

        ele1 = driver.find_element(By.XPATH, "//input[@id='username']")
        ele1.send_keys("lisi")

        ele2 = driver.find_element(By.XPATH, "//input[@id='password']")
        ele2.send_keys("123456")

        ele3 = driver.find_element(By.XPATH, "//button[@id='login_btn']")
        ele3.click()

        time.sleep(3)

        ele4 = driver.find_element(By.XPATH, '/html/body/div/div/aside/div[2]/nav/ul/li[3]/a')
        ele4.click()
        time.sleep(2)
        ele5 = driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/ul/li[2]/a')
        ele5.click()
        time.sleep(2)

        ele6 = driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[1]/input')
        ele6.send_keys("10000")
        time.sleep(2)

        ele7 = driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[3]/input')
        ele7.send_keys("10")
        time.sleep(2)

        ele8 = driver.find_element(By.XPATH,
                                   '/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[4]/button')
        ele8.click()
        time.sleep(2)

        ele9 = driver.find_element(By.XPATH,
                                   '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]')
        ele9.click()
        time.sleep(2)

        driver.find_element(By.XPATH, '/html/body/div/div/header/nav/div/ul/li[1]').click()
        time.sleep(1)

        driver.find_element(By.XPATH, '/html/body/div/div/header/nav/div/ul/li[1]/ul/li[4]/a').click()
        time.sleep(1)

        driver.find_element(By.XPATH, "//input[@id='username']").send_keys("admin")

        driver.find_element(By.XPATH, "//input[@id='password']").send_keys("123456")

        driver.find_element(By.XPATH, "//button[@id='login_btn']").click()

        time.sleep(3)

        driver.find_element(By.XPATH, '/html/body/div/div/aside/div[2]/nav/ul/li[5]/a').click()
        time.sleep(1)

        driver.find_element(By.XPATH, '/html/body/div/div/aside/div[2]/nav/ul/li[5]/ul/li[1]/a').click()
        time.sleep(1)

        driver.find_element(By.XPATH,
                            '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr/td[7]/button[1]').click()
        time.sleep(1)

        driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()
        time.sleep(2)

        driver.find_element(By.XPATH, '/html/body/div/div/header/nav/div/ul/li[1]/a').click()
        time.sleep(1)

        driver.find_element(By.XPATH, '/html/body/div/div/header/nav/div/ul/li[1]/ul/li/a').click()
        time.sleep(1)

        driver.find_element(By.XPATH, "//input[@id='username']").send_keys("lisi")

        driver.find_element(By.XPATH, "//input[@id='password']").send_keys("123456")

        driver.find_element(By.XPATH, "//button[@id='login_btn']").click()

        time.sleep(3)

        driver.find_element(By.XPATH, '/html/body/div/div/aside/div[2]/nav/ul/li[4]/a').click()
        time.sleep(1)

        driver.find_element(By.XPATH, '/html/body/div/div/aside/div[2]/nav/ul/li[4]/ul/li[3]/a').click()
        time.sleep(1)

        connect.commit()
        sql = f'select balance from bankcard where defaultl=1 and userId={id}'
        cursor.execute(sql)
        y = cursor.fetchall()
        money2 = y[0][0]

        if money2 != money1 + 10000:
            driver.save_screenshot("贷款2.jpg")
        self.assertEqual(money1 + 10000, money2)
        driver.quit()

    def testLoan3(self):
        driver = webdriver.Chrome()

        driver.maximize_window()

        driver.get("http://localhost:90")

        time.sleep(2)

        ele1 = driver.find_element(By.XPATH, "//input[@id='username']")
        ele1.send_keys("lisi5")

        ele2 = driver.find_element(By.XPATH, "//input[@id='password']")
        ele2.send_keys("123456")

        ele3 = driver.find_element(By.XPATH, "//button[@id='login_btn']")
        ele3.click()

        time.sleep(3)

        ele4 = driver.find_element(By.XPATH, '/html/body/div/div/aside/div[2]/nav/ul/li[3]/a')
        ele4.click()
        time.sleep(2)
        ele5 = driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/ul/li[2]/a')
        ele5.click()
        time.sleep(2)

        ele6 = driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[1]/input')
        ele6.send_keys("10000")
        time.sleep(2)

        ele7 = driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[3]/input')
        ele7.send_keys("10")
        time.sleep(2)

        ele8 = driver.find_element(By.XPATH,
                                   '/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[4]/button')
        ele8.click()
        time.sleep(2)

        ele9 = driver.find_element(By.XPATH,
                                   '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]')
        ele9.click()
        time.sleep(2)

        x = driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text

        if x != "申请失败，请先绑定个人信息":
            driver.save_screenshot("贷款3.jpg")
        self.assertEqual(x, "申请失败，请先绑定个人信息")
        driver.quit()

    def testLoan4(self):
        driver = webdriver.Chrome()

        driver.maximize_window()

        driver.get("http://localhost:90")

        time.sleep(2)

        ele1 = driver.find_element(By.XPATH, "//input[@id='username']")
        ele1.send_keys("lisi")

        ele2 = driver.find_element(By.XPATH, "//input[@id='password']")
        ele2.send_keys("123456")

        ele3 = driver.find_element(By.XPATH, "//button[@id='login_btn']")
        ele3.click()

        time.sleep(3)

        ele4 = driver.find_element(By.XPATH, '/html/body/div/div/aside/div[2]/nav/ul/li[3]/a')
        ele4.click()
        time.sleep(2)
        ele5 = driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/ul/li[2]/a')
        ele5.click()
        time.sleep(2)

        ele6 = driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[1]/input')
        ele6.send_keys("100000000000000000")
        time.sleep(2)

        ele7 = driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[3]/input')
        ele7.send_keys("10")
        time.sleep(2)

        ele8 = driver.find_element(By.XPATH,
                                   '/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[4]/button')
        ele8.click()
        time.sleep(2)

        ele9 = driver.find_element(By.XPATH,
                                   '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]')
        ele9.click()
        time.sleep(2)

        x = driver.find_element(By.XPATH,
                                '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text

        if x != "申请失败":
            driver.save_screenshot("贷款4.jpg")
        self.assertEqual(x, "申请失败")
        driver.quit()

    def testLoan5(self):
        driver = webdriver.Chrome()

        driver.maximize_window()

        driver.get("http://localhost:90")

        time.sleep(2)

        ele1 = driver.find_element(By.XPATH, "//input[@id='username']")
        ele1.send_keys("lisi")

        ele2 = driver.find_element(By.XPATH, "//input[@id='password']")
        ele2.send_keys("123456")

        ele3 = driver.find_element(By.XPATH, "//button[@id='login_btn']")
        ele3.click()

        time.sleep(3)

        ele4 = driver.find_element(By.XPATH, '/html/body/div/div/aside/div[2]/nav/ul/li[3]/a')
        ele4.click()
        time.sleep(2)
        ele5 = driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/ul/li[2]/a')
        ele5.click()
        time.sleep(2)

        ele6 = driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[1]/input')
        ele6.send_keys("10")
        time.sleep(2)

        ele7 = driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[3]/input')
        ele7.send_keys("10")
        time.sleep(2)

        ele8 = driver.find_element(By.XPATH,
                                   '/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[4]/button')
        ele8.click()
        time.sleep(2)

        ele9 = driver.find_element(By.XPATH,
                                   '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]')
        ele9.click()
        time.sleep(0.5)

        x = driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text

        if x != "申请失败，申请额度必须是100的整数倍":
            driver.save_screenshot("贷款5.jpg")
        self.assertEqual(x, "申请失败，申请额度必须是100的整数倍")
        driver.quit()

    def testLoan6(self):
        driver = webdriver.Chrome()

        driver.maximize_window()

        driver.get("http://localhost:90")

        time.sleep(2)

        ele1 = driver.find_element(By.XPATH, "//input[@id='username']")
        ele1.send_keys("lisi")

        ele2 = driver.find_element(By.XPATH, "//input[@id='password']")
        ele2.send_keys("123456")

        ele3 = driver.find_element(By.XPATH, "//button[@id='login_btn']")
        ele3.click()

        time.sleep(3)

        ele4 = driver.find_element(By.XPATH, '/html/body/div/div/aside/div[2]/nav/ul/li[3]/a')
        ele4.click()
        time.sleep(2)
        ele5 = driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/ul/li[2]/a')
        ele5.click()
        time.sleep(2)

        ele6 = driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[1]/input')
        ele6.send_keys("10")
        time.sleep(2)

        ele7 = driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[3]/input')
        ele7.send_keys("10000")
        time.sleep(2)

        ele8 = driver.find_element(By.XPATH,
                                   '/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[4]/button')
        ele8.click()
        time.sleep(2)

        x = driver.find_element(By.XPATH,'/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[3]/span').text

        if x != "输入期限不合法！":
            driver.save_screenshot("贷款6.jpg")
        self.assertEqual(x, "输入期限不合法！")
        driver.quit()

    def testLoan7(self):
        driver = webdriver.Chrome()

        driver.maximize_window()

        driver.get("http://localhost:90")

        time.sleep(2)

        ele1 = driver.find_element(By.XPATH, "//input[@id='username']")
        ele1.send_keys("lisi")

        ele2 = driver.find_element(By.XPATH, "//input[@id='password']")
        ele2.send_keys("123456")

        ele3 = driver.find_element(By.XPATH, "//button[@id='login_btn']")
        ele3.click()

        time.sleep(3)

        ele4 = driver.find_element(By.XPATH, '/html/body/div/div/aside/div[2]/nav/ul/li[3]/a')
        ele4.click()
        time.sleep(2)
        ele5 = driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/ul/li[2]/a')
        ele5.click()
        time.sleep(2)

        ele6 = driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[1]/input')
        ele6.send_keys("10")
        time.sleep(2)

        ele7 = driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[3]/input')
        ele7.send_keys("9999")
        time.sleep(2)

        ele8 = driver.find_element(By.XPATH,
                                   '/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[4]/button')
        ele8.click()
        time.sleep(2)

        ele9 = driver.find_element(By.XPATH,
                                   '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]')
        ele9.click()
        time.sleep(0.5)

        x = driver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text

        if x != "申请成功!请等待管理员审核":
            driver.save_screenshot("贷款7.jpg")
        self.assertEqual(x, "申请成功!请等待管理员审核")
        driver.quit()



